<?php

/**
 * comments actions.
 *
 * @package    jmscargo
 * @subpackage comments
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class commentsActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->commentss = Doctrine_Core::getTable('Comments')
      ->createQuery('a')
      ->execute();
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new CommentsForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new CommentsForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($comments = Doctrine_Core::getTable('Comments')->find(array($request->getParameter('id'))), sprintf('Object comments does not exist (%s).', $request->getParameter('id')));
    $this->form = new CommentsForm($comments);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($comments = Doctrine_Core::getTable('Comments')->find(array($request->getParameter('id'))), sprintf('Object comments does not exist (%s).', $request->getParameter('id')));
    $this->form = new CommentsForm($comments);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($comments = Doctrine_Core::getTable('Comments')->find(array($request->getParameter('id'))), sprintf('Object comments does not exist (%s).', $request->getParameter('id')));
    $comments->delete();

    $this->redirect('comments/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $comments = new Comments();
      $comments->setBody($form->getValue('body'));
      $comments->setUserId($form->getValue('user_id'));
      $comments->setNewsId($form->getValue('news_id'));
      $comments->save();
      $this->getUser()->setFlash('Sucess','Таны сэтгэгдэл амжилттай орлоо');
      $this->redirect('medee/show?id='.$comments->getNews()->getId());
      
      $message = $this->getMailer()->compose(
      array('enkhee@globaledu.mn' => 'News portal'),
      $comments->getNews()->getUsers()->getEmail(),
      'Tаны мэдээнд сэтгэгдэл ирлээ',
                '
     Эрхэм '.$comments->getUsers()-getFullName().' Таны мэдээн дээр сэтгэгдэл ирлээ
         Хүндэтгэсэн,
         News.mn
         '
     );
 
    $this->getMailer()->send($message);
    }
  }
}
